var e = require('../@babel/runtime/helpers/regeneratorRuntime.js'),
  a = require('../@babel/runtime/helpers/asyncToGenerator.js'),
  t = getApp(),
  r = 1,
  o = 3,
  i = 11;
module.exports = Behavior({
  properties: {},
  options: {},
  observers: {},
  data: { address_id: void 0, remrak_value: '' },
  pageLifetimes: {},
  created: function () {},
  attached: function () {},
  ready: function () {},
  detached: function () {},
  methods: {
    payInit: function () {
      var o = this;
      return a(
        e().mark(function a() {
          var i, n, s, d;
          return e().wrap(function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  return (
                    (i = o.data),
                    (n = i.goodsId),
                    (s = i.num),
                    (d = i.address_id),
                    (e.next = 3),
                    t.$api.weidou
                      .orderConfirm(
                        { goods_id: n, nums: s, address_id: d },
                        { errorKill: !1 },
                      )
                      .then(function (e) {
                        var a = [];
                        (a =
                          Number(e.user_wallet) > Number(e.freight_fee)
                            ? e.pay_method.map(function (e, a) {
                                return (
                                  e.id === r
                                    ? (e.checked = !0)
                                    : (e.checked = !1),
                                  e
                                );
                              })
                            : e.pay_method.map(function (e, a) {
                                return (e.checked = 0 === a), e;
                              })),
                          (e.pay_method = a),
                          (e.addr = e.addr || {}),
                          (e.freight_fee = Number(e.freight_fee)),
                          (e.need_pay_freight_fee = !!Number(e.freight_fee)),
                          o.setData({ pay_data: e });
                      })
                      .catch(function (e) {
                        console.error('weidou.orderConfirm: ', e),
                          '请完善收货地址！' === e.message ||
                            wx.showModal({
                              title: '提示',
                              content: e.message,
                              showCancel: !1,
                            }),
                          o.setData({
                            pay_data: {
                              pay_method: [],
                              addr: {},
                              freight_fee: 0,
                              need_pay_freight_fee: 0,
                            },
                          }),
                          console.error(
                            'weidou.orderConfirm pay_data: ',
                            o.data.pay_data,
                          );
                      })
                  );
                case 3:
                case 'end':
                  return e.stop();
              }
          }, a);
        }),
      )();
    },
    changePayMethod: function (e) {
      for (
        var a = e.currentTarget.dataset.item,
          t = {},
          r = this.data.pay_data.pay_method,
          o = 0;
        o < r.length;
        o++
      ) {
        var i = r[o];
        t['pay_data.pay_method['.concat(o, '].checked')] =
          a.channel === i.channel;
      }
      this.setData(t);
    },
    showPswPop: function (e) {
      this.setData({ popFlag: !0, popType: e ? 'psdIn' : 'setPwd' });
    },
    jumpToDetail: function (e, a, t) {
      !t && wx.showToast({ title: '支付失败', icon: 'none', duration: 800 }),
        setTimeout(function () {
          wx.navigateTo({
            url: '/packageOrder/pages/Weidou/orderDetail/orderDetail?order_id='
              .concat(e, '&order_sn=')
              .concat(a),
          });
        }, 300);
    },
    jumpToAfterPay: function () {},
    closePop: function (e) {
      console.log(e), console.log(this.data);
      var a = this.data.pay_got_data,
        t = a.order_id,
        r = a.order_sn;
      switch (e.detail) {
        case 'secret_cancel':
          this.setData({ popFlag: !1, popType: '' }), this.jumpToDetail(t, r);
          break;
        case 'secret_succeed':
          this.setData({ popFlag: !1, popType: '' }),
            this.jumpToDetail(t, r, !0);
          break;
        case 'secret_fail':
        case 'set_succeed':
          break;
        case 'set_fail':
        case 'set_cancel':
          this.setData({ popFlag: !1, popType: '' }), this.jumpToDetail(t, r);
          break;
        default:
          this.setData({ popFlag: !1, popType: '' });
      }
    },
    afterPwd_walletPay: function (e) {
      console.log(e), (this.data.pay_pwd = e.detail), this.walletPay();
    },
    beforSubmit: function () {
      var r = this;
      return a(
        e().mark(function a() {
          var o, i, n, s, d, c, p, u, _, l, f, h, y, g;
          return e().wrap(function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  if (
                    ((o = r.data),
                    (i = o.pay_data.addr),
                    (n = o.sku_id),
                    (s = o.skuTitle),
                    (d = o.goodsType),
                    (c = o.need_total_weidou),
                    (p = o.num),
                    (u = o.goodsId),
                    (_ = o.remrak_value),
                    i.hasOwnProperty('id') ||
                      'weidou' !== d.type ||
                      1 != d.form)
                  ) {
                    e.next = 4;
                    break;
                  }
                  return (
                    wx.showToast({
                      title: '收货地址不能为空，请选一个',
                      icon: 'none',
                      duration: 2e3,
                    }),
                    e.abrupt('return')
                  );
                case 4:
                  if (n) {
                    e.next = 7;
                    break;
                  }
                  return (
                    wx.showToast({ title: s, icon: 'none', duration: 2e3 }),
                    e.abrupt('return')
                  );
                case 7:
                  if (!('weidou' === d.type && d.microbean < c)) {
                    e.next = 10;
                    break;
                  }
                  return (
                    wx.showToast({
                      title: '可用微豆不足',
                      icon: 'none',
                      duration: 2e3,
                    }),
                    e.abrupt('return')
                  );
                case 10:
                  return (
                    wx.showLoading({ title: '正在获取支付数据...', mask: !0 }),
                    (e.next = 13),
                    t.$api.weidou.orderSubmit({
                      goods_id: u,
                      nums: p,
                      address_id: i.id,
                      sku_id: n,
                      buyer_message: _,
                    })
                  );
                case 13:
                  (l = e.sent),
                    1 == d.form && 1 != l.is_pay
                      ? ((f = l.order),
                        (h = f.id),
                        (y = f.order_sn),
                        r.getPayDetail(h, y))
                      : (l.order.from,
                        wx.hideLoading(),
                        wx.showToast({
                          title: '支付成功',
                          icon: 'success',
                          duration: 800,
                        }),
                        r.close(),
                        (g = r.data.goodsType.form),
                        setTimeout(function () {
                          wx.navigateTo({
                            url: '/pages/center/weidouMall/afterPay/afterPay?order_id='
                              .concat(l.order.id, '&order_sn=')
                              .concat(l.order.order_sn, '&goods_form=')
                              .concat(g),
                          });
                        }, 500));
                case 15:
                case 'end':
                  return e.stop();
              }
          }, a);
        }),
      )();
    },
    remarkInput: function (e) {
      this.setData({ remrak_value: e.detail.value });
    },
    getPayDetail: function (r, o) {
      var i = this;
      return a(
        e().mark(function a() {
          var n, s, d, c, p, u, _, l, f, h, y, g;
          return e().wrap(function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  return (
                    'getPayOrderDetail',
                    (e.next = 3),
                    t.$api.order.getPayOrderDetail({
                      id: r,
                      order_sn: o,
                      api_version: '3.16',
                    })
                  );
                case 3:
                  (n = e.sent),
                    (s = n.detail),
                    n.type,
                    (d = n.is_set_pay_pwd),
                    (c = n.is_secret_free),
                    (p = s.pay_overtime),
                    (u = s.actual_fee),
                    (_ = s.wallet),
                    (l = s.order_sn),
                    (f = s.is_child),
                    (h = s.orderExtend),
                    (y = s.overtime),
                    (g = {
                      is_set_pay_pwd: d,
                      is_secret_free: c,
                      pay_overtime: p,
                      overtime: y,
                      actual_fee: u,
                      payTypes: i.data.pay_data.pay_method,
                      wallet: _,
                      order_sn: l,
                      is_child: f,
                      orderExtend: h,
                      order_id: r,
                    }),
                    i.setData({ actual_fee: u, pay_got_data: g }, function () {
                      i.submit();
                    });
                case 11:
                case 'end':
                  return e.stop();
              }
          }, a);
        }),
      )();
    },
    submit: function () {
      var e = this.data.pay_got_data,
        a = e.payTypes,
        t = e.order_id,
        n = e.wallet,
        s = e.actual_fee,
        d = e.order_sn,
        c = a.find(function (e) {
          return e.checked;
        });
      if (c) {
        if (c.id === r) {
          if (+n > 0 && +n < +s)
            return wx.hideLoading(), void wx.showToast({ title: '余额不足' });
          var p = this.data.pay_got_data,
            u = p.is_set_pay_pwd;
          1 == +p.is_secret_free
            ? this.walletPay()
            : (wx.hideLoading(),
              1 == u ? this.showPswPop(!0) : this.showPswPop(!1));
        }
        c.id === o && this.wxPay(),
          c.id === i &&
            wx.navigateTo({
              url: '/packageOrder/pages/payForOther/payForOther?id='
                .concat(t, '&pageType=mine&order_sn=')
                .concat(d),
            });
      }
    },
    walletPay: function () {
      var e = this,
        a = this.data.pay_got_data,
        o = a.order_sn,
        i = a.order_id,
        n = a.is_set_pay_pwd,
        s = this.data.pay_pwd;
      this.$fetching ||
        ((this.$fetching = !0),
        t.$api.pay
          .orderPay(
            { pay_type: r, order_sn: o, pay_pwd: s },
            { loading: !0, loadingDesc: '支付中' },
          )
          .then(function (a) {
            wx.hideLoading(),
              wx.showToast({ title: '支付成功' }),
              (e.$fetching = !1);
            var t = e.data.goodsType.form;
            e.close(),
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/center/weidouMall/afterPay/afterPay?order_id='
                    .concat(i, '&order_sn=')
                    .concat(o, '&comefrom=detail&goods_form=')
                    .concat(t),
                });
              }, 500);
          })
          .catch(function (a) {
            if ((wx.hideLoading(), (e.$fetching = !1), 1 == n)) {
              var t = e.selectComponent('#PsdPop');
              t && t._upDataStatus('error');
            } else e.jumpToDetail(i, o);
          }));
    },
    wxPay: function () {
      var e = this,
        a = this.data.pay_got_data,
        r = a.order_id,
        i = a.order_sn,
        n = this;
      this.$fetching ||
        ((this.$fetching = !0),
        t.$api.pay
          .orderPay({ order_sn: i, pay_type: o, api_version: '3.16' })
          .then(function (a) {
            e.$fetching = !1;
            var o = a.timeStamp,
              s = a.nonceStr,
              d = a.package,
              c = a.signType,
              p = a.paySign;
            wx.requestPayment({
              timeStamp: o,
              nonceStr: s,
              package: d,
              signType: c,
              paySign: p,
              success: function (e) {
                wx.hideLoading(),
                  wx.showToast({ title: '支付成功' }),
                  t.WX_AD.judgeIsAdIn() &&
                    t.WX_AD.addTask('userActions', {
                      key: 'COMPLETE_ORDER',
                      keyName: '用户下单',
                      action_param: { value: n.data.actual_fee },
                    }),
                  n.close();
                var a = n.data.goodsType.form;
                n.close(),
                  setTimeout(function () {
                    wx.navigateTo({
                      url: '/pages/center/weidouMall/afterPay/afterPay?order_id='
                        .concat(r, '&order_sn=')
                        .concat(i, '&comefrom=detail&goods_form=')
                        .concat(a),
                    });
                  }, 800);
              },
              fail: function (e) {
                n.jumpToDetail(r, i);
              },
            });
          })
          .catch(function (e) {
            (n.$fetching = !1), n.jumpToDetail(r, i);
          }));
    },
  },
});
